package com.kly.user.db.dao.impl;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fqgj.common.base.AbstractBaseMapper;
import com.fqgj.common.utils.CollectionUtils;
import com.fqgj.common.utils.StringUtils;
import com.google.common.collect.Lists;
import com.kly.user.db.dao.UserTurnOnsDao;
import com.kly.user.db.dbo.UserTurnOnsDBO;
import com.kly.user.db.entity.UserTurnOnsEntity;
import com.kly.user.enums.AppCodeEnum;
import org.apache.poi.util.StringUtil;
import org.springframework.stereotype.Repository;

import java.io.Serializable;
import java.util.List;
import java.util.stream.Collectors;

/**
* User: 11437
* Date: 2023-08-01
* Time: 15:13:39.779
*/

@Repository
public class UserTurnOnsDaoImpl extends AbstractBaseMapper<UserTurnOnsEntity> implements UserTurnOnsDao {

    @Override
    public List<UserTurnOnsEntity> listUserTurnOns(final String appCode) {
        if (StringUtils.isEmpty(appCode)) {
            return Lists.newArrayList();
        }
        final List<UserTurnOnsEntity> listUserTurnOns = this.getSqlSession().selectList(this.getStatement("listUserTurnOns"));
        if (CollectionUtils.isEmpty(listUserTurnOns)) {
            return Lists.newArrayList();
        }

        for (UserTurnOnsEntity o : listUserTurnOns) {
            if (StringUtils.isNotEmpty(o.getExtendInfo())) {
                if (!AppCodeEnum.MEBOO.getAppCode().equals(appCode)) {
                    final List<UserTurnOnsDBO> userTurnOns = JSONObject.parseArray(o.getExtendInfo(), UserTurnOnsDBO.class);
                    final UserTurnOnsDBO turnOns = userTurnOns.parallelStream().filter(o1 -> appCode.equals(o1.getAppCode())).findFirst().orElse(null);
                    if (turnOns != null) {
                        o.setImageUrl(turnOns.getUrl());
                    }
                }
            }
        }


        return listUserTurnOns;
    }
}